﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;

namespace Client_Revenue
{
    public partial class Reports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            BindData();
            }
        }

        private void BindData()
        {
         using (var DE = new dominiumEntities())
            {
                //Producers
                var theProducers = (from p in DE.People
                                    join i in DE.SalesInfoes on p.PersonId equals i.PersonId
                                    join r in DE.SalesRoles on i.SalesRoleId equals r.SalesRoleId
                                    where r.RoleName == ("Producer") | r.RoleName == ("Executive Sponsor")
                                    orderby p.LastName
                                    select new
                                    {
                                        p.PersonId,
                                        FullName = p.FirstName + " " + p.LastName
                                    }).Distinct().ToList();

                ddProducers.DataSource = theProducers;
                ddProducers.DataTextField = "FullName";
                ddProducers.DataValueField = "PersonId";
                ddProducers.Items.Insert(0, new ListItem("Select Producer", "0"));
                ddProducers.SelectedIndex = 0;
                ddProducers.DataBind();
             
               
                gvProducerPayout.DataSource= DE.GetAllCommissionPaidToProducers();
                gvProducerPayout.DataBind();
            }
        
        }

        protected void ddProducers_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (var DE = new dominiumEntities())
            {
                if (Convert.ToInt32(ddProducers.SelectedValue) != 0)
                {
                    gvProducerPayout.DataSource = DE.GetAllCommissionPaidPerProducer(Convert.ToInt32(ddProducers.SelectedValue));
                }
                else
                {
                    gvProducerPayout.DataSource = DE.GetAllCommissionPaidToProducers();
                }
                gvProducerPayout.DataBind();
            }

        }

        protected void btnExport2Excel_Click(object sender, EventArgs e)
        {
            this.gvProducerPayout.AllowPaging = false;
            this.gvProducerPayout.AllowSorting = false;
            this.gvProducerPayout.EditIndex = -1;

            // Let's bind data to GridView
            this.BindData();

            // Let's output HTML of GridView
            Response.Clear();
            Response.ContentType = "application/vnd.xls";
            Response.AddHeader("content-disposition",
                    "attachment;filename=ProducerPayOut.xls");

            StringWriter swriter = new StringWriter();
            HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

            HtmlForm frm = new HtmlForm();
            this.gvProducerPayout.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(this.gvProducerPayout);
            frm.RenderControl(hwriter);

            Response.Write(swriter.ToString());
            Response.End();
        }
    }
}